<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class OrderTableMigration extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $table = $this->table('order', [
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_general_ci'
        ]);

        $table->addColumn('order_no', 'string', [
            'limit' => 32,
            'null' => false,
            'comment' => '订单号'
        ])
        ->addColumn('user_id', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '用户ID'
        ])
        ->addColumn('total_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '订单总金额'
        ])
        ->addColumn('pay_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '实付金额'
        ])
        ->addColumn('discount_amount', 'decimal', [
            'precision' => 10,
            'scale' => 2,
            'null' => false,
            'default' => 0.00,
            'comment' => '优惠金额'
        ])
        ->addColumn('status', 'integer', [
            'limit' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '订单状态：0待付款，1已付款，2已发货，3已完成，4已取消，5已退款'
        ])
        ->addColumn('pay_status', 'integer', [
            'limit' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '支付状态：0未支付，1已支付，2已退款'
        ])
        ->addColumn('pay_time', 'integer', [
            'limit' => 11,
            'null' => true,
            'comment' => '支付时间'
        ])
        ->addColumn('pay_method', 'string', [
            'limit' => 20,
            'null' => true,
            'comment' => '支付方式：wechat, alipay, score'
        ])
        ->addColumn('delivery_status', 'integer', [
            'limit' => 2,
            'null' => false,
            'default' => 0,
            'comment' => '发货状态：0未发货，1已发货，2已签收'
        ])
        ->addColumn('delivery_time', 'integer', [
            'limit' => 11,
            'null' => true,
            'comment' => '发货时间'
        ])
        ->addColumn('receiver_name', 'string', [
            'limit' => 50,
            'null' => true,
            'comment' => '收货人姓名'
        ])
        ->addColumn('receiver_phone', 'string', [
            'limit' => 20,
            'null' => true,
            'comment' => '收货人电话'
        ])
        ->addColumn('receiver_address', 'text', [
            'null' => true,
            'comment' => '收货地址'
        ])
        ->addColumn('remark', 'text', [
            'null' => true,
            'comment' => '订单备注'
        ])
        ->addColumn('created_at', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '创建时间'
        ])
        ->addColumn('updated_at', 'integer', [
            'limit' => 11,
            'null' => false,
            'comment' => '更新时间'
        ])
        ->addIndex(['order_no'], ['unique' => true])
        ->addIndex(['user_id'])
        ->addIndex(['status'])
        ->addIndex(['created_at'])
        ->create();
    }
}
